package com.cyf.mq.producer;

import com.cyf.mq.constant.MqConstant;
import com.cyf.mq.pojo.HeroDTO;
import com.cyf.mq.util.JacksonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

/**
 * @author chenyifan
 * @create 2025-02-18 15:11
 */
@Slf4j
public class SimpleProducer {

    public static void main(String[] args) throws Exception{
        // 创建生产者，指定生产者组
        DefaultMQProducer producer = new DefaultMQProducer(MqConstant.MESSAGE_PRODUCER_GROUP_A);
        // 指定nameServer地址
        producer.setNamesrvAddr(MqConstant.NAME_SERVER_ADDR);
        // 启动生产者
        producer.start();

        // 创建消息
        HeroDTO heroDTO = new HeroDTO("jialuo", "伽罗");
        Message message = new Message(MqConstant.MESSAGE_TOPIC_King, JacksonUtil.toJsonString(heroDTO).getBytes());

        // 发送消息
        SendResult sendResult = producer.send(message);
        log.info("发送成功：{}", sendResult);
        // 关闭生产者
        producer.shutdown();
    }
}
